package com.ruoyi.project.system.controller;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.SiyuBlacklist;
import com.ruoyi.project.system.domain.SiyuCharge;
import com.ruoyi.project.system.mapper.SiyuBlacklistMapper;
import com.ruoyi.project.system.mapper.SiyuChargeMapper;
import com.ruoyi.project.system.mapper.SysUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;
import java.math.BigDecimal;
import java.util.List;
import static com.ruoyi.common.utils.http.HttpUtils.sendRisk;

@RestController
@RequestMapping("/system/black")
public class SiyuBlackListController extends BaseController {

    @Autowired
    private SiyuBlacklistMapper siyuBlacklistMapper;

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SiyuChargeMapper siyuChargeMapper;

    @Log(title = "查询财务列表", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @GetMapping("/listCaiwu")
    public TableDataInfo listCaiwu(SiyuBlacklist siyuBlacklist) {
        startPage();
        List<SiyuBlacklist> list = siyuBlacklistMapper.selectSiyuBlacklistlistCaiwu(siyuBlacklist);
        return getDataTable(list);
    }

    @Log(title = "查询财务", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @GetMapping("caiwu/{id}")
    public AjaxResult caiwu(@PathVariable("id") Integer id) {
        return success(siyuBlacklistMapper.selectSiyuBlacklistCaiwuById(id));
    }

    @Log(title = "插入财务", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @PostMapping("/caiwu")
    public AjaxResult addcaiwu(@RequestBody SiyuBlacklist sbl) {
        sbl.setRemark("查财务");
        sendRisk(sbl);
        return success(siyuBlacklistMapper.insertSiyuCaiwu(sbl));
    }

    @Log(title = "修改财务", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @PutMapping("/caiwu")
    public AjaxResult editcaiwu(@RequestBody SiyuBlacklist siyuBlacklist) {
        return toAjax(siyuBlacklistMapper.updateSiyuCaiwu(siyuBlacklist));
    }

    @Log(title = "移除财务", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @DeleteMapping("caiwu/{ids}")
    public AjaxResult removecaiwu(@PathVariable Integer[] ids) {
        return toAjax(siyuBlacklistMapper.deleteSiyuCaiwuByIds(ids));
    }

    @Log(title = "查询撸子列表", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @GetMapping("/listLuzi")
    public TableDataInfo listLuzi(SiyuBlacklist siyuBlacklist) {
        startPage();
        List<SiyuBlacklist> list = siyuBlacklistMapper.selectSiyuBlacklistlistLuzi(siyuBlacklist);
        return getDataTable(list);
    }

    @Log(title = "查询撸子", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @GetMapping("luzi/{id}")
    public AjaxResult luzi(@PathVariable("id") Integer id) {
        return success(siyuBlacklistMapper.selectSiyuBlacklistLuziById(id));
    }

    @Log(title = "插入撸子", businessType = BusinessType.INSERT)
    @PreAuthorize("@ss.hasPermi('system:luzi:lzcw')")
    @PostMapping("/luzi")
    public AjaxResult addluzi(@RequestBody SiyuBlacklist sbl) {
        sbl.setRemark("查撸子");
        sendRisk(sbl);
        return success(siyuBlacklistMapper.insertSiyuLuzi(sbl));
    }

    @Log(title = "修改撸子", businessType = BusinessType.UPDATE)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @PutMapping("/luzi")
    public AjaxResult editluzi(@RequestBody SiyuBlacklist siyuBlacklist) {
        return toAjax(siyuBlacklistMapper.updateSiyuLuzi(siyuBlacklist));
    }

    @Log(title = "移除撸子", businessType = BusinessType.DELETE)
    @PreAuthorize("@ss.hasPermi('system:luzi:adm')")
    @DeleteMapping("luzi/{ids}")
    public AjaxResult removeluzi(@PathVariable Integer[] ids) {
        return toAjax(siyuBlacklistMapper.deleteSiyuLuziByIds(ids));
    }


    @Log(title = "商户撸子查询", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:luzi:lzcw')")
    @GetMapping("/listOfButton")
    public AjaxResult listOfButton(@NotBlank String type, @NotBlank String name) {


        if ("siyu_caiwu".equals(type) || "siyu_luzi".equals(type)) {
            List<SiyuBlacklist> list;

            Long userId = SecurityUtils.getUserId();
            BigDecimal balance = sysUserMapper.selectUserById(userId.longValue()).getBalance();
            // 写入扣费记录
            BigDecimal price = new BigDecimal(0.1);
            if (balance.compareTo(price) < 0) {
                return error("余额不足！请充值");
            }
            siyuChargeMapper.insertSiyuCharge(new SiyuCharge(
                    userId.intValue(),
                    balance,
                    price,
                    balance.subtract(price),
                    "财务撸子查询"
            ));
            // 减余额
            sysUserMapper.subtractBalance(userId.longValue(), price);
            if ("siyu_caiwu".equals(type)) {
                list = siyuBlacklistMapper.selectSiyuBlacklistByName("siyu_caiwu", name);
                if (list == null || list.size() == 0) {
                    SiyuBlacklist sbl = new SiyuBlacklist(1, name);
                    siyuBlacklistMapper.insertSiyuCaiwu(sbl);
                    sbl.setRemark("查财务");
                    sendRisk(sbl);
                }
                for (SiyuBlacklist blacklist : list) {
                    blacklist.setQueryNum(blacklist.getQueryNum() + 1);
                    siyuBlacklistMapper.addQueryNum("siyu_caiwu", blacklist.getId());
                }
            } else {
                list = siyuBlacklistMapper.selectSiyuBlacklistByName("siyu_luzi", name);
//                if (list == null || list.size() == 0) {
//                    SiyuBlacklist sbl = new SiyuBlacklist(1, name);
//                    siyuBlacklistMapper.insertSiyuLuzi(sbl);
//                    sbl.setRemark("查撸子");
//                    sendRisk(sbl);
//                }
                for (SiyuBlacklist blacklist : list) {
                    blacklist.setQueryNum(blacklist.getQueryNum() + 1);
                    siyuBlacklistMapper.addQueryNum("siyu_luzi", blacklist.getId());
                }
            }
            for (SiyuBlacklist siyuBlacklist : list) {
                siyuBlacklist.setId(null);
            }
            return success(list);
        } else {
            return error();
        }
    }

    @Log(title = "商户撸子核验", businessType = BusinessType.OTHER)
    @PreAuthorize("@ss.hasPermi('system:luzi:lzcw')")
    @GetMapping("/hitLuzi")
    public AjaxResult hitLuzi(@NotBlank String type, @NotBlank String name) {
        if ("siyu_luzi".equals(type)) {
            List<SiyuBlacklist> list = siyuBlacklistMapper.selectSiyuBlacklistByName("siyu_luzi", name);
            for (SiyuBlacklist siyuBlacklist : list) {
                siyuBlacklist.setId(null);
            }
            return success(list);
        } else {
            return error();
        }
    }

}
